package com.zjhcsoft.common.service.upload.impl;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.zjhcsoft.common.object.UploadObj;
import com.zjhcsoft.common.object.UploadPathObj;
import com.zjhcsoft.common.service.upload.AbstractUploadFile;
import com.zjhcsoft.common.service.upload.FileUploadService;

public class FileUploadServiceImpl extends AbstractUploadFile implements FileUploadService{
	private static final long serialVersionUID = 1349090410023337654L;

	public UploadObj uploadFile(FileInputStream input,String fileName,String model_path){
		// TODO Auto-generated method stub
        InputStream in = new BufferedInputStream(input); 
        UploadObj obj = super.uploadFile(in, fileName,model_path);
        return obj;
	}
	
	public UploadObj uploadFile(File file,String model_path) throws FileNotFoundException {
		// TODO Auto-generated method stub
		InputStream in = new BufferedInputStream(new FileInputStream(file));
		UploadObj obj = super.uploadFile(in, file.getName(),model_path);
		return obj;
	}

	
	public UploadObj uploadImgFile(FileInputStream input,String fileName, int img_type,String model_path) throws IOException {
		// TODO Auto-generated method stub
		InputStream in = new BufferedInputStream(input);
		String ext = getFileExt(fileName);
		UploadObj obj = null;
		if (super.isImg(ext)){
			obj = super.uploadFile(in,fileName,model_path);
			if (img_type == AbstractUploadFile.SMALL_IMG){
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createSmallImg(f);
//				obj.setSmall_img_path(super.createSmallImg(f));
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
			}
		}
		return obj;
	}

	public UploadObj uploadImgFile(File file, int img_type,String model_path) throws IOException {
		// TODO Auto-generated method stub
		InputStream in = new BufferedInputStream(new FileInputStream(file));
		String ext = getFileExt(file.getName());
		UploadObj obj = null;
		if (super.isImg(ext)){
			obj = super.uploadFile(in, file.getName(),model_path);
			if (img_type == AbstractUploadFile.SMALL_IMG){
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createSmallImg(f);
//				obj.setSmall_img_path(super.createSmallImg(f));
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
			}
		}
		return obj;
	}
	

	
	
	public UploadObj uploadImgFile(File file,String model_path) throws IOException {
		// TODO Auto-generated method stub
		return this.uploadImgFile(file, AbstractUploadFile.SMALL_IMG, model_path);
	}

	public UploadObj uploadImgFile(FileInputStream input,String fileName,String model_path) throws IOException {
		// TODO Auto-generated method stub
		return this.uploadImgFile(input,fileName, AbstractUploadFile.SMALL_IMG, model_path);
	}
	
	public UploadObj uploadImgFile(FileInputStream input,String fileName,int length,int img_type,String model_path) throws IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		String ext = super.getFileExt(fileName);
		UploadObj obj = null;
			obj = this.uploadFile( input, fileName,model_path);
			if (img_type == AbstractUploadFile.SMALL_IMG){
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createSmallImg(f,length);
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
			}
			if (img_type == AbstractUploadFile.SUB_IMG){
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createCutImg(f, 0, 0, length, length);
				obj.setSub_img_path(pathObj.getRealPath());
				obj.setRelativeSubImgPath(pathObj.getRelativePath());
			}
		return obj;
	}
	


	public UploadObj uploadImgFile(File file, int x, int y, int wid, int hig,
			int img_type,String model_path) throws IOException {
		// TODO Auto-generated method stub
		String ext = super.getFileExt(file.getName());
		UploadObj obj = null;
		//if (super.isImg(ext)){
			obj = this.uploadFile(file,model_path);

			if (img_type == AbstractUploadFile.SMALL_IMG){
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createSmallImg(f,wid,hig);
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
			}
			if (img_type == AbstractUploadFile.SUB_IMG){
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createCutImg(f, x, y, wid, hig);
				obj.setSub_img_path(pathObj.getRealPath());
				obj.setRelativeSubImgPath(pathObj.getRelativePath());
			}
			
		//}
		return obj;
	}
	

	public UploadObj uploadImgFile(FileInputStream input,String fileName, int x , int y , int wid, int hig ,int img_type,String model_path) throws IOException{
		// TODO Auto-generated method stub
		String ext = super.getFileExt(fileName);
		UploadObj obj = null;
		//if (super.isImg(ext)){
			obj = this.uploadFile( input, fileName,model_path);
			if (img_type == AbstractUploadFile.SMALL_IMG){
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createSmallImg(f,wid,hig);
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
			}
			if (img_type == AbstractUploadFile.SUB_IMG){
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createCutImg(f, x, y, wid, hig);
				obj.setSub_img_path(pathObj.getRealPath());
				obj.setRelativeSubImgPath(pathObj.getRelativePath());
			}
			
		//}
		return obj;
		
	}
	
	

}
